#include "stdafx.h"
#include "NXCircleModel.h"
#include <vector>
using namespace std;

BOOL AddRadOutline(float fW,float fH,float fStart,float fEnd,vector<POINTF>& vOutline)
{
	float fCenter = (fStart+fEnd)*0.5f;
	float fRW = fW*0.5f;
	float fRH = fH*0.5f;
	float fStartX = cos(fStart)*fRW;
	float fStartY = -sin(fStart)*fRH;
	float fEndX = cos(fEnd)*fRW;
	float fEndY = -sin(fEnd)*fRH;

	float fRadCenterX = cos(fCenter)*fRW;
	float fRadCenterY = -sin(fCenter)*fRH;

	float fCenterX = (fStartX+fEndX)*0.5f;
	float fCenterY = (fStartY+fEndY)*0.5f;

	float fDis2 = (fCenterX-fRadCenterX)*(fCenterX-fRadCenterX)+(fCenterY-fRadCenterY)*(fCenterY-fRadCenterY);
	
	if(fDis2<0.01f)
	{
		POINTF pPoint1;
		pPoint1.x = fStartX;
		pPoint1.y = fStartY;
		vOutline.push_back(pPoint1);
		pPoint1.x = fRadCenterX;
		pPoint1.y = fRadCenterY;
		vOutline.push_back(pPoint1);
	}
	else
	{
		AddRadOutline(fW,fH,fStart,fCenter,vOutline);
		AddRadOutline(fW,fH,fCenter,fEnd,vOutline);
	}
	return TRUE;
}

BOOL NXCreateCircle(float fWidth ,float fHeight, float fStartAngle,float fEndAngle,eCircleType type,OUT TPOUTLINEFIGURE& figure)
{
	vector<POINTF>	vCirclePoint;
	AddRadOutline(fWidth,fHeight,fStartAngle,fEndAngle,vCirclePoint);
	if(type == eCIRCLE_FAN)
	{
		POINTF pointEnd;
		pointEnd.x =  cos(fEndAngle)*fWidth*0.5f;
		pointEnd.y =  -sin(fEndAngle)*fHeight*0.5f;
		vCirclePoint.push_back(pointEnd);
		pointEnd.x = 0.0f;
		pointEnd.y = 0.0f;
		vCirclePoint.push_back(pointEnd);
	}
	else if(type == eCIRCLE_ARC)
	{
		POINTF pointEnd;
		pointEnd.x =  cos(fEndAngle)*fWidth*0.5f;
		pointEnd.y =  -sin(fEndAngle)*fHeight*0.5f;
		vCirclePoint.push_back(pointEnd);
	}
	ASSERT(vCirclePoint.size() > 0);
	POINTF point = vCirclePoint[0];
	vCirclePoint.push_back(point);

	int nPoint = (int)vCirclePoint.size();
	figure.nPointCount = nPoint;
	figure.nDirect = 0;
	figure.pdwType = new DWORD[nPoint];
	figure.pfX = new float[nPoint];
	figure.pfY = new float[nPoint];
	for (int i=0;i<nPoint;i++)
	{
		POINTF point = vCirclePoint[i];
		figure.pdwType[i] = TP_PRIM_LINE;
		figure.pfX[i] = point.x;
		figure.pfY[i] = point.y;
	}
	figure.pdwType[0] = TP_PRIM_FIGURESTART;

	return TRUE;
}


